package com.wewins.fota.mapper;

import com.wewins.fota.bean.bo.filter.FilterCond;
import com.wewins.fota.bean.po.FotaLog;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface FotaLogMapper {

    @Select({
            "<script>",
            "SELECT * FROM fota_log WHERE 1=1 ",
            "<if test=\"product != null and product != ''\" >",
            "AND product=#{product}",
            "</if>",
            "<if test=\"imei != null and imei != ''\" >",
            "AND imei=#{imei}",
            "</if>",
            "<if test=\"srcV != null and srcV != ''\" >",
            "AND from_v=#{srcV}",
            "</if>",
            "<if test=\"dstV != null and dstV != ''\" >",
            "AND to_v=#{dstV}",
            "</if>",
            "<if test=\"operator != null and operator != -1\" >",
            "AND operator=#{operator}",
            "</if>",
            "<if test=\"auto != null and auto != -1\" >",
            "AND auto_download=#{auto}",
            "</if>",
            "<if test=\"userIp != null and userIp != ''\" >",
            "AND req_server=#{userIp}",
            "</if>",
            "<if test='minCreateTime != null and minCreateTime != 0' >",
            "<![CDATA[ AND create_time>=#{minCreateTime} ]]>",
            "</if>",
            "<if test='maxCreateTime != null and maxCreateTime != 0' >",
            "<![CDATA[ AND create_time<=#{maxCreateTime} ]]>",
            "</if>",
            "<if test='minUpdateTime != null and minUpdateTime != 0' >",
            "<![CDATA[ AND update_time>=#{minUpdateTime} ]]>",
            "</if>",
            "<if test='maxUpdateTime != null and maxUpdateTime != 0' >",
            "<![CDATA[ AND update_time<=#{maxUpdateTime} ]]>",
            "</if>",
            "</script>"
    })
    @Results(id = "fotaLogMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "fromV", column = "from_v"),
            @Result(property = "toV", column = "to_v"),
            @Result(property = "autoDownload", column = "auto_download"),
            @Result(property = "reqServer", column = "req_server"),
            @Result(property = "respServer", column = "resp_server"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<FotaLog> queryAll(FilterCond cond);

    @Select("SELECT * FROM fota_log WHERE id = #{id}")
    @ResultMap({"fotaLogMap"})
    FotaLog findById(long id);

    @Insert("INSERT INTO fota_log " +
            "(product, imei, req_server, operator, auto_download, from_v, to_v, create_time, update_time, resp_server, count) " +
            "VALUES " +
            "(#{product}, #{imei}, #{reqServer}, #{operator}, #{autoDownload}, #{fromV}, #{toV}, #{createTime}, #{updateTime}, #{respServer}, #{count}) " +
            "ON DUPLICATE KEY UPDATE " +
            "req_server=#{reqServer}, auto_download=#{autoDownload}, update_time=#{updateTime}, resp_server=#{respServer}, count=count + #{count}"
    )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(FotaLog fotaLog);

    @Update("UPDATE fota_log SET " +
            "product=#{product}, imei=#{imei}, req_server=#{reqServer}, operator=#{operator}, auto_download=#{autoDownload}, " +
            "from_v=#{fromV}, to_v=#{toV}, create_time=#{createTime}, update_time=#{updateTime}, resp_server=#{respServer}, count=#{count} " +
            "WHERE id = #{id}"
    )
    int updateByWholeQuantity(FotaLog fotaLog);

    @Update("UPDATE fota_log SET " +
            "req_server=#{reqServer}, auto_download=#{autoDownload}, " +
            "update_time=#{updateTime}, resp_server=#{respServer}, count=count + #{count} " +
            "where product=#{product} and imei=#{imei} and from_v=#{fromV} and to_v=#{toV} and operator=#{operator}"
    )
    int updateByIncrement(FotaLog fotaLog);

    @Delete("DELETE FROM fota_log WHERE id = #{id}")
    int delete(FotaLog fotaLog);

    @Select("select * from fota_log where product=#{product} and imei=#{imei} order by create_time desc limit 1")
    @ResultMap({"fotaLogMap"})
    FotaLog findLatestLog4check(String product, String imei);

    @Select("select count(*) from fota_log where product=#{product} and imei=#{imei} and from_v=#{fromV} and to_v=#{toV} and operator=#{operator}")
    int existLog(FotaLog fotaLog);

    @Select("select count(*) from fota_log where product=#{product} and imei=#{imei} and from_v=#{fromV}")
    int hasUpdated(String product, String imei, String fromV);

    @Select({"call insert_or_update_fota_log(#{product}, #{imei}, #{reqServer}, #{operator}, #{autoDownload}, #{fromV}, #{toV}, #{createTime}, #{respServer}, #{count})"})
    @Options(statementType = StatementType.CALLABLE)
    void insertOrUpdate(FotaLog fotaLog);
}
